<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .layui-upload-img { width: 90px; height: 90px; margin: 0; }
            .pic-more { width:100%; left; margin: 10px 0px 0px 0px;}
            .pic-more li { width:90px; float: left; margin-right: 5px;}
            .pic-more li .layui-input { display: initial; }
            .pic-more li a { position: absolute; top: 0; display: block; }
            .pic-more li a i { font-size: 24px; background-color: #008800; }
            #slide-pc-priview .item_img img{ width: 90px; height: 90px;}
            #slide-pc-priview li{position: relative;}
            #slide-pc-priview li .operate{ color: #000; display: none;}
            #slide-pc-priview li .toleft{ position: absolute;top: 40px; left: 1px; cursor:pointer;}
            #slide-pc-priview li .toright{ position: absolute;top: 40px; right: 1px;cursor:pointer;}
            #slide-pc-priview li .close{position: absolute;top: 5px; right: 5px;cursor:pointer;}
            #slide-pc-priview li:hover .operate{ display: block;}
            .item-list {
                display: flex;
                flex-direction: row;
                border-bottom: 1px solid #ccc;
            }

            .item-list .item {
                width: 100px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                cursor: pointer;
            }

            .item-active {
                border-bottom: 2px solid #2d8cf0;
            }

            .part {
                display:  none;
            }

            .part-0 {
                display: block;
            }

        </style>
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">

                    <div class="layui-form-item">
                        <label class="layui-form-label">活动名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">活动描述</label>
                        <div class="layui-input-block">
                            <textarea name="desc" id="desc" cols="30" rows="4" class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">收费标准</label>
                        <div class="layui-input-block">
                            <input type="text" name="price" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">最低参与人数</label>
                        <div class="layui-input-block">
                            <input type="number" name="min_count" value="" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">最多可接待人数</label>
                        <div class="layui-input-block">
                            <input type="number" name="max_count" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">行程开始时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="start_time" id="start_time" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">行程结束时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="end_time" id="end_time" autocomplete="off" value="" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item" id="pics">
                        <div class="layui-form-label">相册图集</div>
                        <div class="layui-input-block" style="width: 70%;">
                            <div class="layui-upload">
                                <button type="button" class="layui-btn layui-btn-primary pull-left" id="slide-pc">选择多图</button>
                                <div class="pic-more">
                                    <ul class="pic-more-upload-list" id="slide-pc-priview">
                                        <?php foreach ($images as $image) : ?>
                                        <li class="item_img">
                                            <div class="operate">
                                                <i class="toleft layui-icon">
                                                    <img style="width: 16px; height: 16px;" src="/app/admin/admin/images/toleft.png">
                                                </i>
                                                <i class="toright layui-icon">
                                                    <img style="width: 16px; height: 16px;" src="/app/admin/admin/images/toright.png">
                                                </i>
                                                <i  class="close layui-icon">
                                                    <img style="width: 16px; height: 16px;" src="/app/admin/admin/images/close.png">
                                                </i>
                                            </div>
                                            <img src="<?=$image?>" class="img" ><input type="hidden" name="images[]" value="<?=$image?>" />
                                        </li>
                                        <?php endforeach ?>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">景点介绍</label>
                        <div class="layui-input-block">
                            <textarea name="content" id="content" class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">行程安排</label>
                        <div class="layui-input-block">
                            <textarea name="schedule" id="schedule" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/activity/select" + location.search;
            const UPDATE_API = "/app/admin/activity/update";

            let $ = layui.$;
            var upload = layui.upload;
            upload.render({
                elem: '#slide-pc',
                url: '/app/admin/upload/image',
                exts: 'jpg|png|jpeg',
                multiple: true,
                before: function (obj) {
                    layer.msg('图片上传中...', {
                        icon: 16,
                        shade: 0.01,
                        time: 0
                    })
                },
                done: function (res) {
                    layer.close(layer.msg());//关闭上传提示窗口
                    if (res.status == 0) {
                        return layer.msg(res.message);
                    }
                    //$('#slide-pc-priview').append('<input type="hidden" name="pc_src[]" value="' + res.filepath + '" />');
                    $('#slide-pc-priview').append('<li class="item_img"><div class="operate"><i class="toleft layui-icon"><img style="width: 16px; height: 16px;" src="/app/admin/admin/images/toleft.png"></i><i class="toright layui-icon"><img style="width: 16px; height: 16px;" src="/app/admin/admin/images/toright.png"></i><i  class="close layui-icon"><img style="width: 16px; height: 16px;" src="/app/admin/admin/images/close.png"></i></div><img src="' + res.data.url + '" class="img" ><input type="hidden" name="thumbs[]" value="' + res.data.url + '" /></li>');
                }
            });

            //点击多图上传的X,删除当前的图片
            $("body").on("click",".close",function(){
                var li_index=$(this).closest("li").index();
                $(this).closest("li").remove();
            });
            //多图上传点击<>左右移动图片
            $("body").on("click",".pic-more ul li .toleft",function(){
                var li_index=$(this).closest("li").index();
                if(li_index>=1){
                    $(this).closest("li").insertBefore($(this).closest("ul").find("li").eq(Number(li_index)-1));
                }
            });
            $("body").on("click",".pic-more ul li .toright",function(){
                var li_index=$(this).closest("li").index();
                $(this).closest("li").insertAfter($(this).closest("ul").find("li").eq(Number(li_index)+1));
            });

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            } else {
                                obj.attr("value", value);
                            }
                        });
                        
                        // 字段 发布时间 created_time
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#start_time",
                                type: "datetime",
                            });

                            layui.laydate.render({
                                elem: "#end_time",
                                type: "datetime",
                            });
                        })

                        layui.use(['tinymce'], function() {
                            var tinymce = layui.tinymce
                            editor = tinymce.render({
                                elem: "#content",
                                images_upload_url: "/app/admin/upload/image",
                                height: 400
                            });

                            editor2 = tinymce.render({
                                elem: "#schedule",
                                images_upload_url: "/app/admin/upload/image",
                                height: 400
                            });

                        });
                        
                        
                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render()
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                // 字段验证允许为空
                layui.form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data.field['content'] = editor.getContent()
                    data.field['schedule'] = editor2.getContent()
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });

        </script>

    </body>

</html>
